import { reqGoodsInfo,reqAddOrUpdateShopCart} from "@/api"
// 引入临时游客身份(随机字符串，不能更换)
import {getUUID} from '@/utils'

const state = {
    goodInfo:{},
    // 游客临时身份
    uuid_token:getUUID()
}
const mutations = {
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo
    }
}
const actions = {
    // 获取产品信息的action
    async getGoodInfo({commit},skuId){
        let result = await reqGoodsInfo(skuId)
        if(result.code == 200){
            commit('GETGOODINFO',result.data)
        }
    },
    // 将商品添加到购物车中
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        // 加入购物车返回的结果,结果是一个promise
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        // 服务器加入购物车成功
        if(result.code == 200){
            return "ok"
        }else{
            // 加入购物车失败
            return Promise.reject(new Error('faile'))
        }
    }
}
const getters = {
    // 路径导航简化的数据
    categoryView(state){
        // state.goodInfo初始状态是空对象，所以他的categoryView属性值是undefined
        return state.goodInfo.categoryView || {}
    },
    // 简化产品信息的数据
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    //产品售卖属性的简化
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList || []
    },
}

export default{
    state,
    mutations,
    actions,
    getters
}